package com.uchengwang.ui.base_ui.ui;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.uchengwang.R;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.wenming.swipebacklayout.SwipeBackLayout;
import com.wenming.swipebacklayout.Utils;
import com.wenming.swipebacklayout.app.SwipeBackActivityBase;
import com.wenming.swipebacklayout.app.SwipeBackActivityHelper;
import com.zhy.autolayout.config.AutoLayoutConifg;

/**
 * Created by U城网 on 2017/2/27.
 */
public class BaseActivity extends AllBaseActivity  implements SwipeBackActivityBase {
    public final RelativeLayout.LayoutParams mmLp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    protected ActivityTitleBar titleBar;
    private RelativeLayout contentView;
    private int color = Color.WHITE;
    private final String PACKAGE_URL_SCHEME = "package:";

    private SwipeBackActivityHelper mHelper;
    private ImageView ivShadow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AutoLayoutConifg.getInstance().useDeviceSize();

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mHelper = new SwipeBackActivityHelper(this);//手势滑动结束界面
        mHelper.onActivityCreate();

        getSwipeBackLayout().setSwipeMode(SwipeBackLayout.FULL_SCREEN_LEFT);
        getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        getSwipeBackLayout().setSensitivity(BaseActivity.this, 0.3f);
    }


    @Override
    public void setContentView(int layoutResID) {

        ViewGroup allView = (ViewGroup) getContainer();
        super.setContentView(allView);//兼顾滑动


        View view = LayoutInflater.from(this).inflate(R.layout.base_activity, null);
        contentView = (RelativeLayout) view.findViewById(R.id.base_content_view);
        titleBar = (ActivityTitleBar) view.findViewById(R.id.title_bar);
        View v = LayoutInflater.from(this).inflate(layoutResID, null);
        contentView.addView(v, mmLp);

        allView.addView(view);
    }

    public ActivityTitleBar getRy() {
        return titleBar;
    }

    @Override
    public void setContentView(View view) {
        this.setContentView(view, mmLp);
    }

    public void setBackgroundColor(int color) {
        this.color = color;
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(R.layout.base_activity);
        contentView = (RelativeLayout) findViewById(R.id.base_content_view);
        titleBar = (ActivityTitleBar) findViewById(R.id.title_bar);
        contentView.addView(view, params);
    }


    private View getContainer() {
        RelativeLayout container = new RelativeLayout(this);
        ivShadow = new ImageView(this);
        ivShadow.setBackgroundColor(getResources().getColor(R.color.swipe_bg_color));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        container.addView(ivShadow, params);
        return container;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mHelper.onPostCreate();
    }

    @Override
    public View findViewById(int id) {
        View v = super.findViewById(id);
        if (v == null && mHelper != null)
            return mHelper.findViewById(id);
        return v;
    }


    @Override
    public com.wenming.swipebacklayout.SwipeBackLayout getSwipeBackLayout() {
        return mHelper.getSwipeBackLayout();
    }

    @Override
    public void setSwipeBackEnable(boolean enable) {
        getSwipeBackLayout().setEnableGesture(enable);
    }

    @Override
    public void scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this);
        getSwipeBackLayout().scrollToFinishActivity();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
